package org.electric.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;




public class JDBCUtil {
	
	private static String DRIVER_CLASS;
	
	private static String URL;
	
	private static String USERRNAME;
	
	private static String PASSWORD;
	
	private static Properties p=new Properties();
	
	 static{
		 	try {
		 		
		 			ClassLoader classLoader = JDBCUtil.class.getClassLoader();
		 			InputStream fis =classLoader.getResourceAsStream("JDBC.properties");
					p.load(fis);
					DRIVER_CLASS=p.getProperty("driver");
					URL=p.getProperty("url");
					USERRNAME=p.getProperty("user");
					PASSWORD=p.getProperty("pass");
					Class.forName(DRIVER_CLASS);
					fis.close();
		 	} catch (IOException e) {
			 		e.printStackTrace();
		 	} catch (ClassNotFoundException e) {
					e.printStackTrace();
		 	}
	 	}
	 
	 public static Connection getConection(){
		 
		 			Connection conn=null;
		 			
		 			try{
		 				conn=DriverManager.getConnection(URL, USERRNAME,PASSWORD);		
		 				
//		 				conn.setAutoCommit(false);
		 			}catch (Exception e) {
		 					e.printStackTrace();
		 			}
		 			return conn;
	 }
	 
	 public static void close(Connection conn,PreparedStatement pre,ResultSet ... rs) {
			try {
						if(rs.length!=0&&rs[0]!=null){
							rs[0].close();
						}
						if(pre!=null){
							pre.close();
						}	
						if(conn!=null){
							conn.close();
						}
			} catch (Exception e) {
						e.printStackTrace();
			}
}
	
	
}
